<?php
namespace Framework\SlickGrid;
/**
	Use this class when you're displaying columns from a single Doctrine domain entity. The domain model should
	have accessors defined for all the commonly used properties. For example, an Applicant should have shortcut 
	getters for the ID Year field, even though the ID Year is a property of the Program Cycle, not the Applicant.
*/
class DoctrineGridDataProvider extends GridDataProvider {
	public function render($returnObj=false) {
		$columns = $this->columns->getColumns();
		$hiddenColumns = $this->columns->getHiddenColumns();
		if($columns === null) {
			$buffer = array();
			// Use the default entity JSON representation
			foreach($this->data as $row) {
				$buffer[] = $row->getJSON();
			}
			return '['.implode(',',$buffer).']';
		} else {
			// Get only the specified columns
			$buffer = array();
			foreach($this->data as $row) {
				$out = array();
				foreach($columns as $columnName => $columnDef) {
					if(is_array($columnDef['getter'])) {
						$method = $columnDef['getter'][0]; // could use assoc keys, but it gets overly verbose when declaring a column if you do
						$params = $columnDef['getter'][1];
						$out[$columnName] = call_user_func_array(array($row, $method), $params);
					}
					else {
						$out[$columnName] = $row->$columnDef['getter']();
					}
				}
				foreach($hiddenColumns as $hiddenColumn => $columnGetter) {
					$out[$hiddenColumn] = $row->$columnGetter();
				}
				$buffer[] = $out;
			}
			if($returnObj) {
				return $buffer;
			} else {
				return json_encode($buffer);
			}
		}
	}
}